package org.example.student_dormitory.service.impl;

import org.example.student_dormitory.dao.AdminManagerDao;
import org.example.student_dormitory.dao.impl.AdminManagerDaoImpl;
import org.example.student_dormitory.dto.Result;
import org.example.student_dormitory.entity.Admin;
import org.example.student_dormitory.service.AdminManagerService;

import java.util.List;

public class AdminManagerServiceImpl implements AdminManagerService {
    //创建数据持久层对象
    private AdminManagerDao adminManagerDao = new AdminManagerDaoImpl();
    @Override
    public Result loadtable(String page, String pageSize) {
        //数据转换
        int page_int = Integer.parseInt(page);
        int pageSize_int = Integer.parseInt(pageSize);
//        System.out.println("已进入Service,准备进入Dao");
        List<Admin> adminList = adminManagerDao.selectAll(page_int, pageSize_int);
//        System.out.println("准备返回Result对象");
        //返回数据
        if (adminList == null) {
            //没有数据
//            System.out.println("666没数据");
            return new Result<>(100,"没有数据",null);
        }else {
            //有数据
//            System.out.println("666有数据");
            return new Result<>(200,"查询到数据集", adminList);
        }
    }

    @Override
    public Result loadtable(String page, String pageSize, String key, String value) {
        //数据转换
        int page_int = Integer.parseInt(page);
        int pageSize_int = Integer.parseInt(pageSize);
        List<Admin> adminList = null;
        //判断是普通分页还是带关键字分页
        if (value == null || "".equals(value)) {
            //普通分页
//            System.out.println("普通分页");
            adminList = adminManagerDao.selectAll(page_int,pageSize_int);
        }else {
            //带关键字搜索分页
//            System.out.println("关键字分页");
            adminList = adminManagerDao.selectAll(page_int, pageSize_int,key,value);
        }
        if (adminList == null || adminList.size() == 0){
//            System.out.println("666没数据");
            return new Result<>(100, "没有数据", null);
        }else {
//            System.out.println("666有数据");
            return new Result<>(200, "有数据结果", adminList);
        }
    }

    @Override
    public Result checkUsername(String username) {
        boolean b = adminManagerDao.selectByUsername(username);
        if (b) {
//            System.out.println(200);
            return new Result<>(200, "用户名可用", null);
        }else {
//            System.out.println(100);
            return new Result<>(100, "用户名不可用", null);
        }
    }

    @Override
    public Result addAdmin(String username, String password, String name, String sex, String phone, String role) {
//        System.out.println("进入Service");
        Admin admin = new Admin();

        admin.setUsername(username);
        admin.setPassword(password);
        admin.setName(name);
//        System.out.println(1);
//        String gender = Integer.parseInt(sex) == 1 ? "男":"女";
//        System.out.println(2);
        admin.setGender(sex);
//        System.out.println(3);
        admin.setPhone(phone);
        admin.setDr(Long.parseLong(role));

//        System.out.println("准备进入Dao");
        boolean insert = adminManagerDao.insert(admin);
//        System.out.println("已从Dao中返回");
        if (insert) {
            return new Result<>(200,"添加成功", null);
        }else {
            return new Result<>(100,"添加失败", null);
        }
    }

    @Override
    public Result deleteById(String id) {
        Boolean delete = adminManagerDao.deleteById(Integer.parseInt(id));
        if (delete){
            return new Result<>(200,"删除成功", null);
        }else {
            return new Result<>(100,"删除失败", null);
        }
    }

    @Override
    public Result updateAdmin(String id, String username, String password, String name, String gender, String phone, String role) {
//        System.out.println("已经进入Service");
        Admin admin = new Admin();
        admin.setId(Integer.parseInt(id));
        admin.setUsername(username);
        admin.setPassword(password);
        admin.setName(name);
        admin.setGender(gender);
        admin.setPhone(phone);
        if ("系统管理员".equals(role)){
            admin.setDr(0);
        }else if ("宿舍管理员".equals(role)){
            admin.setDr(1);
        }
//        System.out.println("准备进入Dao");
        boolean update = adminManagerDao.update(admin);
        if (update){
//            System.out.println("修改成功");
            return new Result<>(200,"修改成功",null);
        }else {
//            System.out.println("修改失败");
            return new Result<>(100,"修改失败",null);
        }
    }

    @Override
    public Result deleteList(String[] ids) {
        int[] ids_int = new int[ids.length];
        for (int i = 0; i < ids.length; i++) {
            ids_int[i] = Integer.parseInt(ids[i]);
        }
        boolean b = adminManagerDao.deleteList(ids_int);
        if (b){
            return new Result<>(200,"成功",null);
        }else {
            return new Result<>(100,"失败",null);
        }
    }
}
